﻿using CatUtils.Utils.Currency;
using CatUtils.Utils.Currency.IoUtils;
using CatUtils.Utils.Currency.TextUtils;

using CatUtils.Utils.SpecialUtils.Html;
using CatUtilsDll.Utils.Currency.WebUtils;
using Newtonsoft.Json;
using RabbitDownload.Helper;
using RabbitDownload.Helper.Currency;

using RabbitDownload.WebSite.Local.TagManger;
using System;
using System.Collections.Generic;
using System.Net;
using static RabbitDownload.WebSite.Local.TagManger.LocalTagStatcivalue;

namespace RabbitDownload.WebSite.Pixiv.PixivDic
{
    internal class PixivDicTranslate
    {
        /// <summary>
        ///翻译有词库文件的文件夹
        /// </summary>
        /// <param name="FloderPath"></param>
        /// <returns></returns>
        public void TranslateFloder(String FloderPath, CWebclient cWebclient)
        {
            List<string> FileList = FloderNameHelper.GetFullFileNameList(FloderPath);
            int CompltedCont = 0;
            foreach (string SingleFile in FileList)
            {
                new PrintLog().PercentLog(FileList.Count, CompltedCont++);
                TranslateFile(SingleFile, cWebclient);
            }
        }

        /// <summary>
        ///   处理合并词库，词库去重复
        /// </summary>
        /// <param name="FileList"></param>
        public void HandleLexicon(List<string> FileList)
        {
            List<string> ALLTagJoin = new List<string>();//全部标签合并
            List<string> JpTagList = new List<string>();//日文列表
            List<string> ZhTagList = new List<string>();//中文列表
            List<string> ZhTwTagList = new List<string>();//中文繁体列表
            List<string> EnTagList = new List<string>();//英文列表
            List<string> RomaTagList = new List<string>();//罗马音列表
            List<string> KoTagList = new List<string>();//韩文列表
            List<string> UnTranslateList = new List<string>();//未翻译列表

            string AllTagSavePath = "";
            foreach (String SingleFile in FileList)
            {
                int ComltedCount = 0;
                AllTagSavePath = FloderNameHelper.GetFloderPath(SingleFile) + "词库合并.txt";
                string TempTagInfoText = FileHelper.ReadAllText(SingleFile);
                List<string> TempTagInfoList = HtmlHelper.GetHtmlValueList(TempTagInfoText, "//tag");

                TempTagInfoList.AddRange(HtmlHelper.GetHtmlValueList(TempTagInfoText, "//item"));
                foreach (string TagInfo in TempTagInfoList)
                {
                    TranslateStruct translateStruct = new TranslateStruct(TagInfo);

                    ALLTagJoin.Add(TagInfo);
                    new PrintLog().PercentLog(TempTagInfoList.Count, ++ComltedCount);
                    int TranslateCount = 0;

                    if (translateStruct.JpTag != "")
                        TranslateCount++;
                    if (translateStruct.ZhTag != "")
                        TranslateCount++;
                    if (translateStruct.ZhtwTag != "")
                        TranslateCount++;
                    if (translateStruct.EnTag != "")
                        TranslateCount++;
                    if (translateStruct.RomaTag != "")
                        TranslateCount++;
                    if (translateStruct.KoTag != "")
                        TranslateCount++;

                    if (TranslateCount == 1)
                    {
                        UnTranslateList.Add(TagInfo);
                        continue;
                    }

                    if (translateStruct.JpTag != "")
                        JpTagList.Add(TagInfo);
                    if (translateStruct.ZhTag != "")
                        ZhTagList.Add(TagInfo);
                    if (translateStruct.ZhtwTag != "")
                        ZhTwTagList.Add(TagInfo);
                    if (translateStruct.EnTag != "")
                        EnTagList.Add(TagInfo);
                    if (translateStruct.KoTag != "")
                        KoTagList.Add(TagInfo);
                    if (translateStruct.RomaTag != "")
                        RomaTagList.Add(TagInfo);
                }
            }
            Console.WriteLine("保存路径：" + AllTagSavePath);
            if (AllTagSavePath == "")
                return;
            AutoSaveList(AllTagSavePath, "_所有标签", ALLTagJoin);
            AutoSaveList(AllTagSavePath, "_日文翻译", JpTagList);
            AutoSaveList(AllTagSavePath, "_中文翻译", ZhTagList);
            AutoSaveList(AllTagSavePath, "_繁体中文翻译", ZhTwTagList);
            AutoSaveList(AllTagSavePath, "_英文翻译", EnTagList);
            AutoSaveList(AllTagSavePath, "_罗马音翻译", RomaTagList);
            AutoSaveList(AllTagSavePath, "_韩文翻译", KoTagList);
            AutoSaveList(AllTagSavePath, "_暂无翻译", UnTranslateList);
        }

        private void AutoSaveList(string FilePath, string FileTip, List<string> TagList)
        {
            if (TagList.Count == 0)
                return;
            FileHelper.WriteList(FileNameHelper.CreateFileNameAddTip(FilePath, FileTip), TagList);
        }

        /// <summary>
        /// 翻译词库文件
        /// </summary>
        /// <param name="FileName"></param>
        public void TranslateFile(string FileName, CWebclient cWebclient)
        {
            List<String> TagList = FileHelper.ReadAllLine(FileName);
            int CompltedCont = 0;

            List<string> TranslateList = new List<string>();
            foreach (string SigleTagInfo in TagList)
            {
                string Jptag
                    = HtmlHelper.GetFirstText(SigleTagInfo, "//tagname");
                new PrintLog().PercentLog(TagList.Count, CompltedCont++);
                if (Jptag == "")
                    continue;

                TranslateRoot translateRoot = GetTranslate(Jptag, cWebclient);
                if (translateRoot != null)
                {
                    TranslateList.Add(string.Format(TextMode, Jptag, translateRoot.zh, translateRoot.zh_tw, "", translateRoot.ko, translateRoot.en));
                }
            }
            FileHelper.WriteList(FileNameHelper.CreateFileNameAddTip(FileName, "_Translate"), TranslateList);
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="SingleTag"></param>
        /// <returns></returns>
        private TranslateRoot GetTranslate(string SingleTag, CWebclient cWebclient)
        {
            try
            {
                TranslateRoot translateRoot = GetALLTranslate(SingleTag, cWebclient);
                if (translateRoot != null)
                    return translateRoot;

                //GetALLTranslate(SingleTag, cWebclient).body.zh;
            }
            catch (Exception ex)
            {
                PrintLog.Log(ex);
            }
            return null;
        }

        /// <summary>
        /// 获取所有翻译
        /// </summary>
        /// <param name="singleTag"></param>
        /// <returns></returns>
        private TranslateRoot GetALLTranslate(string singleTag, CWebclient cWebclient)
        {
            string Api = "https://www.pixiv.net/ajax/search/tags/" + WebHelper.UrlEncode(singleTag) + "?lang=zh";
            try
            {
                WebHeaderCollection webHeaderCollection = WebHelper.PcWebHeader();
                webHeaderCollection.Add("Referer", Api);
                string Shtml = cWebclient.GetHtml(Api, webHeaderCollection);
                Shtml = TextHelper.RegexText(Shtml, "{\"en\".*?\"}");
                Console.WriteLine(Shtml);
                TranslateRoot translateRoot = JsonConvert.DeserializeObject<TranslateRoot>(Shtml);
                return translateRoot;
            }
            catch (Exception ex)
            {
                PrintLog.Log(ex);
            }
            return null;
        }

        public class TranslateRoot
        {
            /// <summary>
            ///
            /// </summary>
            public string en { get; set; }

            /// <summary>
            ///
            /// </summary>
            public string ko { get; set; }

            /// <summary>
            ///
            /// </summary>
            public string zh { get; set; }

            /// <summary>
            ///
            /// </summary>
            public string zh_tw { get; set; }

            /// <summary>
            ///
            /// </summary>
            public string romaji { get; set; }
        }
    }
}